/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "gl_local.h"

/*-----------------------------------------------------------------------------
	GL texture stuff :
-----------------------------------------------------------------------------*/

//
//	EGLTexture::~EGLTexture 
//
EGLTexture::~EGLTexture ( void )
{
	GL_CALL( glDeleteTextures(1, &gl_tex_obj) );
}


//
//	EGLTexture::CreateTextureFromFile
//
IPxTexture	EGLTexture::CreateTextureFromFile ( const char *path )
{
	IPxTexture	tex;

	try {
		LOGF("Loading : %s", path);

		//	Virtual texture names :
		if (path[0]=='*') {	
			if (strcmp (path, "*null")		== 0 )	tex = CreateNullTexture ( );					    else
			if (strcmp (path, "*noise")		== 0 )	tex = CreateNoiseTexture( );					    else
			if (strcmp (path, "*black")		== 0 )	tex = CreateSolidTexture( 0x00, 0x00, 0x00, 0xFF ); else
			if (strcmp (path, "*grey")		== 0 )	tex = CreateSolidTexture( 0x80, 0x80, 0x80, 0xFF ); else
			if (strcmp (path, "*white")		== 0 )	tex = CreateSolidTexture( 0xFF, 0xFF, 0xFF, 0xFF ); else
			if (strcmp (path, "*red")		== 0 )	tex = CreateSolidTexture( 0xFF, 0x00, 0x00, 0xFF ); else
			if (strcmp (path, "*green")		== 0 )	tex = CreateSolidTexture( 0x00, 0xFF, 0x00, 0xFF ); else
			if (strcmp (path, "*blue")		== 0 )	tex = CreateSolidTexture( 0x00, 0x00, 0xFF, 0xFF ); else
			if (strcmp (path, "*flat")		== 0 )	tex = CreateSolidTexture( 0x80, 0x80, 0xFF, 0x80 ); else	// flat normal map
			if (strncmp( path, "*color", 6)	== 0)	tex = CreateColorTexture( path ); else
			RUNTIME_ERROR(va("wrong virtual texture name '%s'", path));
		} else {	
			tex	=	LoadTextureFromFile( path );
		}

	} catch (exception &e) {	
		LOG_WARNING(e.what());	

		tex = CreateNullTexture( );
	}

	return tex;
}


//
//	EGLTexture::LoadTextureFromFile
//
IPxTexture EGLTexture::LoadTextureFromFile ( const char *path )
{
	IPxImage img = ifct()->LoadImageFromFile(path);	

	return LoadTextureFromImage( img );
}


//
//	EGLTexture::LoadTextureFromImage
//
IPxTexture EGLTexture::LoadTextureFromImage( const IPxImage img )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	//get the image width and height
	tex->width	= img->GetWidth();
	tex->height	= img->GetHeight();
	tex->depth	= 0;
	tex->pitch	= tex->width*tex->height*4;
	tex->type	= RS_TEXTURE_2D;
	tex->format	= RS_FORMAT_ARGB_U8;

	GLenum internal_format	= GL_RGBA8;
	GLenum format			= GL_BGRA;
	GLenum type				= GL_UNSIGNED_BYTE;					
 
	tex->CreateGLTexture2D(GL_TEXTURE_2D, internal_format, format, type, img->GetByteRGBABlob()->GetBufferPointer());

	return px_tex;
}


//
//	EGLTexture::UpdateFromImage
//
void EGLTexture::UpdateFromImage( const IPxImage image )
{
}


//
//	EGLTexture::WriteDataRaw
//
void EGLTexture::WriteDataRaw3D( const float * data )
{
	GL_CALL( glBindTexture( GL_TEXTURE_3D,  gl_tex_obj ) );
	GL_CALL( glTexSubImage3D(GL_TEXTURE_3D, 0,  // level
		0, 0, 0, // ofs
		width, height, depth, 
		GL_RED, GL_FLOAT, data) );
	GL_CALL( glBindTexture( GL_TEXTURE_3D,  0 )	);
}


//
//	EGLTexture::CreateTexture2D
//
IPxTexture	EGLTexture::CreateTexture2D	( uint w, uint h, ERSFormat_t frmt )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= w;
	tex->height = h;
	tex->depth	= 1;
	tex->format	= frmt;
	tex->type	= RS_TEXTURE_2D;

	switch(frmt) 
	{
		case RS_FORMAT_XRGB_U8: 
		{
			tex->pitch	= w * 3;
			tex->depth	= 1;

			std::vector<byte> gl_tex_data(tex->pitch * tex->height, 0);
			tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, &gl_tex_data.front());

			break;
		}
		case RS_FORMAT_ARGB_U8: 
		{
			tex->pitch	= w * 4;
			tex->depth	= 1;

			std::vector<byte> gl_tex_data(tex->pitch * tex->height, 0);
			tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, &gl_tex_data.front());

			break;
		}
		case RS_FORMAT_ARGB_F32:
		{
			tex->pitch	= w * 4;
			tex->depth	= 1;

			std::vector<float> gl_tex_data(tex->pitch * tex->height, 0);
			tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGBA, GL_RGBA, GL_FLOAT, &gl_tex_data.front());
			break;
		}
		case RS_FORMAT_R_F32:
		{
			tex->pitch	= w;
			tex->depth	= 1;

			std::vector<float> gl_tex_data(tex->pitch * tex->height, 0);
			tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_R32F, GL_RED, GL_FLOAT, &gl_tex_data.front());
			break;
		}
		default:
		{
			RUNTIME_ERROR(va("Such texture can't be created: %d", frmt));
		}
	}

	return px_tex;
}


//
//	EGLTexture::CreateTexture2D
//
IPxTexture EGLTexture::CreateTexture2D( uint w, uint h, const EColor *data )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= w;
	tex->height = h;
	tex->depth	= 1;
	tex->format	= RS_FORMAT_ARGB_F32;
	tex->type	= RS_TEXTURE_2D;

	tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGBA32F, GL_RGBA, GL_FLOAT, data);
	return px_tex;
}


//
//	EGLTexture::CreateNullTexture
//
IPxTexture	EGLTexture::CreateNullTexture ( void )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= 16;
	tex->height = 16;
	tex->format	= RS_FORMAT_ARGB_U8;
	tex->type	= RS_TEXTURE_2D;
	tex->depth	= 1;
	tex->pitch	= tex->width * 4;

	std::vector<byte> gl_tex_data(tex->pitch * tex->height, 0);

	byte *ptr = static_cast<byte*>(&gl_tex_data.front());

	for (uint x=0; x<16; x++) {
		for (uint y=0; y<16; y++) {

			byte clr[4];

			// All surface should be BLACK :
			clr[0] = clr[1] = clr[2] = 0x00;
			clr[3] = 0xFF;

			if ((x^y)&1) {				// PINK squares, it is so emotionally
				clr[0] = clr[2] = 0xFF;	
			}

			if (x==15 || y==15) {		// YELLOW edge to view mapping.
				clr[0] = clr[1] = 0xFF;
				clr[2] = 0x00;
			}

			ptr[ y*tex->pitch + x*4 + 0 ] = clr[0];
			ptr[ y*tex->pitch + x*4 + 1 ] = clr[1];
			ptr[ y*tex->pitch + x*4 + 2 ] = clr[2];
			ptr[ y*tex->pitch + x*4 + 3 ] = clr[3];
		}
	}

	tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, &gl_tex_data.front());

	return px_tex;
}



IPxTexture EGLTexture::CreateNoiseTexture( void )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= 64;
	tex->height	= 64;
	tex->format	= RS_FORMAT_ARGB_U8;
	tex->type	= RS_TEXTURE_2D;
	tex->depth	= 1;
	tex->pitch	= tex->width * 4;

	std::vector<byte> gl_tex_data(tex->pitch * tex->height, 0);

	byte *ptr = static_cast<byte*>(&gl_tex_data.front());

	for (uint x=0; x<64; x++) {
		for (uint y=0; y<64; y++) {
			ptr[ y*tex->pitch + x*4 + 0 ] = (byte)rand();
			ptr[ y*tex->pitch + x*4 + 1 ] = (byte)rand();
			ptr[ y*tex->pitch + x*4 + 2 ] = (byte)rand();
			ptr[ y*tex->pitch + x*4 + 3 ] = (byte)rand();
		}
	}

	tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, &gl_tex_data.front());

	return px_tex;
}


//
//	EGLTexture::CreateSolidTexture
//
IPxTexture EGLTexture::CreateSolidTexture( uchar r, uchar g, uchar b, uchar a )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= 16;
	tex->height	= 16;
	tex->format	= RS_FORMAT_ARGB_U8;
	tex->type	= RS_TEXTURE_2D;
	tex->depth	= 1;
	tex->pitch	= tex->width * 4;

	std::vector<byte> gl_tex_data(tex->pitch * tex->height, 0);

	byte *ptr = static_cast<byte*>(&gl_tex_data.front());

	for (uint x=0; x<16; x++) {
		for (uint y=0; y<16; y++) {
			byte clr[4] = {r,g,b,a};

			ptr[ y*tex->pitch + x*4 + 0 ] = clr[0];
			ptr[ y*tex->pitch + x*4 + 1 ] = clr[1];
			ptr[ y*tex->pitch + x*4 + 2 ] = clr[2];
			ptr[ y*tex->pitch + x*4 + 3 ] = clr[3];
		}
	}

	tex->CreateGLTexture2D(GL_TEXTURE_2D, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, &gl_tex_data.front());

	return px_tex;
}


//
//
//
IPxTexture EGLTexture::CreateColorTexture( const char *path )
{
	char strr[3] = "00";
	char strg[3] = "00";
	char strb[3] = "00";
	char stra[3] = "00";
	
	strncpy( strr, &path[ 6], 2 );
	strncpy( strg, &path[ 8], 2 );
	strncpy( strb, &path[10], 2 );
	strncpy( stra, &path[12], 2 );
	
	uint r=0, g=0, b=0, a=0;
	sscanf( strr, "%X", &r);
	sscanf( strg, "%X", &g);
	sscanf( strb, "%X", &b);
	sscanf( stra, "%X", &a);
	
	//LOGF(" %X %X %X %X", r,g,b,a);
	
	return CreateSolidTexture(r,g,b,a);
}


//
//	EGLTexture::WriteDataRaw2D
//
void EGLTexture::WriteDataRaw2D ( uint level, const void *data, uint pitch )
{
	if (type != RS_TEXTURE_2D) {
		RUNTIME_ERROR(__FUNCTION__"() : not a 2D texture");
	}


	GL_CALL( glBindTexture(GL_TEXTURE_2D, gl_tex_obj) );

	GL_CALL( glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pitch/4, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data) );

	GL_CALL( glBindTexture(GL_TEXTURE_2D, 0) );
}

//
//	EGLTexture::CreateGLTexture1D
//
void EGLTexture::CreateGLTexture1D(GLenum target, GLint internal_format, GLenum format, GLenum type, const GLvoid *pixels)
{
	GL_CALL( glGenTextures( 1,			   &gl_tex_obj )	);
	GL_CALL( glBindTexture( GL_TEXTURE_1D,  gl_tex_obj )	);

	glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL,	0 );
	glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER,	GL_LINEAR );
	glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER,	GL_LINEAR_MIPMAP_LINEAR ); 
	glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S,		GL_REPEAT );
	

	GL_CALL( glTexImage1D(GL_TEXTURE_1D, 0, internal_format, width, 0, format, type, pixels) );

	GL_CALL( glGenerateMipmap( GL_TEXTURE_1D ) );

	GL_CALL( glBindTexture(GL_TEXTURE_1D, 0) );
}

//
//	EGLTexture::CreateGLTexture2D
//
void EGLTexture::CreateGLTexture2D(GLenum target, GLint internal_format, GLenum format, GLenum type, const GLvoid *pixels)
{
	GL_CALL( glGenTextures( 1,			   &gl_tex_obj )	);
	GL_CALL( glBindTexture( GL_TEXTURE_2D,  gl_tex_obj )	);

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL,	0 );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,	GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,	GL_LINEAR_MIPMAP_LINEAR ); 
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,		GL_REPEAT );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,		GL_REPEAT );
	
	float max_anisotropy = 1.0f; 
	glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
	
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,	max_anisotropy );

	GL_CALL( glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, pixels) );

	GL_CALL( glGenerateMipmap( GL_TEXTURE_2D ) );

	GL_CALL( glBindTexture(GL_TEXTURE_2D, 0) );
}


//
//	EGLTexture::CreateGLTexture3D
//
void EGLTexture::CreateGLTexture3D( GLenum target, GLint internal_format, GLenum format, GLenum type, const GLvoid *pixels )
{
	GL_CALL( glGenTextures( 1,			   &gl_tex_obj )	);
	GL_CALL( glBindTexture( GL_TEXTURE_3D,  gl_tex_obj )	);

	glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL,	0 );
	glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER,	GL_LINEAR );
	glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER,	GL_LINEAR_MIPMAP_LINEAR ); 
	glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S,		GL_REPEAT );
	glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T,		GL_REPEAT );
	
	float max_anisotropy = 1.0f; 
	glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
	
	glTexParameterf( GL_TEXTURE_3D, GL_TEXTURE_MAX_ANISOTROPY_EXT,	max_anisotropy );

	GL_CALL( glTexImage3D(GL_TEXTURE_3D, 0, internal_format, width, height, depth, 0, format, type, pixels) );

	GL_CALL( glGenerateMipmap( GL_TEXTURE_3D ) );

	GL_CALL( glBindTexture(GL_TEXTURE_3D, 0) );
}


//
//	EGLTexture::CreateVolumeF4
//
IPxTexture EGLTexture::CreateVolumeF4( uint w, uint h, uint d, const EColor *float_data )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= w;
	tex->height	= h;
	tex->depth	= d;
	tex->format	= RS_FORMAT_ARGB_F32;
	tex->type	= RS_TEXTURE_3D;
	tex->pitch	= tex->width * 4;

	tex->CreateGLTexture3D( GL_TEXTURE_3D, GL_RGBA32F, GL_RGBA, GL_FLOAT, float_data );
	
	return px_tex;
}

//
//	EGLTexture::CreateVolumeF
//
IPxTexture EGLTexture::CreateVolumeF( uint w, uint h, uint d, const float *float_data )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= w;
	tex->height	= h;
	tex->depth	= d;
	tex->format	= RS_FORMAT_R_F32;
	tex->type	= RS_TEXTURE_3D;
	tex->pitch	= tex->width;

	tex->CreateGLTexture3D( GL_TEXTURE_3D, GL_R32F, GL_RED, GL_FLOAT, float_data );
	
	return px_tex;
}

//
//	EGLTexture::CreateColormapTexture
//
IPxTexture	EGLTexture::CreateColormapTexture( int n, const EColor *colors )
{
	IPxTexture	px_tex	= new EGLTexture();
	EGLTexture	*tex	= px_tex.to<EGLTexture>();

	tex->width	= n;
	tex->height	= 1;
	tex->depth	= 1;
	tex->format	= RS_FORMAT_ARGB_U8;
	tex->type	= RS_TEXTURE_1D;

	tex->CreateGLTexture1D( GL_TEXTURE_1D, GL_RGBA8, GL_RGBA, GL_FLOAT, colors );

	return px_tex;
}

